package cn.easybuy.dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.Easybuy_NewsDao;
import cn.easybuy.entity.Easybuy_News;

public class Easybuy_NewsDaoImpl extends BaseDao implements Easybuy_NewsDao {

	@Override
	public List<Easybuy_News> getNews() {
		List<Easybuy_News> list = new ArrayList<Easybuy_News>();
		ResultSet rs = null;
		String sql = "select * from easybuy_news ";
		rs = this.exxcuteQuery(sql);
		Easybuy_News easybuy_News = null;
		try {
			while (rs.next()) {
				easybuy_News = new Easybuy_News();
				easybuy_News.setId(rs.getInt("id"));
				easybuy_News.setTitle(rs.getString("title"));
				easybuy_News.setContent(rs.getString("content"));
				easybuy_News.setCreateTime(rs.getString("createTime"));
				list.add(easybuy_News);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(connection, pstmt, rs);
		}
		return list;

	}

	@Override
	public int getTotalCount() {
		ResultSet rs = null;

		int count = -1;
		String sql = " SELECT COUNT('id') FROM  `easybuy_news` ";
		rs = this.exxcuteQuery(sql);
		try {
			rs.next();
			count = rs.getInt(1);
			closeAll(connection, null, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	@Override
	public List<Easybuy_News> getAllnewsByPage(int pageNo, int pageSize) {
		List<Easybuy_News> newsList = new ArrayList<Easybuy_News>();
		ResultSet rs = null;
		String sql = " SELECT * FROM `easybuy_news` ORDER BY createTime DESC limit ?,?";
		rs = this.exxcuteQuery(sql, (pageNo - 1) * pageSize, pageSize);
		try {
			while (rs.next()) {
				Easybuy_News news = new Easybuy_News();
				news.setId(rs.getInt("id"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setCreateTime(rs.getString("createTime"));
				newsList.add(news);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeAll(connection, null, rs);
		}

		return newsList;
	}

	@Override
	public List<Easybuy_News> findAllNewsByTid(int id) {
		ResultSet rs = null;
		List<Easybuy_News> list = new ArrayList<Easybuy_News>();
		String sql = "select * from Easybuy_News where id=?  order by  createTime desc ";
		try {
			rs = this.exxcuteQuery(sql, id);
			Easybuy_News news = null;
			while (rs.next()) {
				news = new Easybuy_News();
				news.setId(rs.getInt("id"));
				news.setTitle(rs.getString("title"));
				news.setContent(rs.getString("content"));
				news.setCreateTime(rs.getString("createTime"));
				list.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();

		} finally {
			closeAll(connection, pstmt, rs);
		}
		return list;
	}

}
